package com.springboot.securtiy.server.impl;

import com.springboot.securtiy.server.MyService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
* Description: 自定义权限访问控制权限
* date: 2023/12/4 15:18<br/>
* @author: zhangzaijun <br/>
* @since JDK 1.8
*/

@Component
public class MyServiceImpl implements MyService {
    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        Object object = authentication.getPrincipal();

        if(object instanceof UserDetails){
            UserDetails user = (UserDetails) object;
            Collection<? extends GrantedAuthority> authorities = user.getAuthorities();

            return authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));
        }
        return false;
    }
}
